const util = require('../../../utils/util.js');
const api = require('../../../config/api.js');
const user = require('../../../utils/user.js');


const app = getApp()


// pages/ucenter/group/group.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    checke:1,
    address:'',
    lat:'',
    lng:'',
    phone: ''
  },

  getPhoneNumber(e) {
    let that = this
    console.log(e.detail.errMsg)
    console.log(e.detail.iv)
    console.log(e.detail.encryptedData)
    util.request(api.Phone, {
      encryptedData: e.detail.encryptedData,
      iv: e.detail.iv
    }, 'get', {
        'Content-Type': 'application/json',
        'X-Qxmall-Token': wx.getStorageSync('token')
      }).then(function (res) {
        console.log(res)
        wx.setStorageSync("phone", res.data.phone.phoneNumber)
        that.setData({
          phone: res.data.phone.phoneNumber
        })
      });

  },
  getwz() {
    let that = this
    wx.chooseLocation({
      success: function (res) {
        console.log(res)
        that.setData({
          address: res.address,
          lat: res.latitude,
          lng: res.longitude
        })
      },
    })
  },
  back_houtai: function (e) {
    if (e.detail.value.leaderName==''){
      wx.showToast({
        title: '团长姓名不能为空',
        icon:'none'
      })
      return false
    }
    if (this.data.address == '') {
      wx.showToast({
        title: '详细地址不能为空',
        icon: 'none'
      })
      return false
    }
    if (e.detail.value.storeName == '') {
      wx.showToast({
        title: '店铺名字不能为空',
        icon: 'none'
      })
      return false
    }
    if (this.data.phone == '') {
      wx.showToast({
        title: '手机号码不能为空',
        icon: 'none'
      })
      return false
    }
    if (app.globalData.hasLogin == true) {
    let that=this
    util.request(api.Group, {
      address: that.data.address,
      geometry: {
        lat: that.data.lat,
        lng: that.data.lng
      },
      leaderName: e.detail.value.leaderName,
      storeName: e.detail.value.storeName,
      isOpen: that.data.checke,
      storePhone: that.data.phone
    }, 'post', {
        'Content-Type': 'application/json',
        'X-Qxmall-Token': wx.getStorageSync('token')
      }).then(function (res) {
        console.log("sass"+res)
        if(res.code==200){
          wx.showModal({
            title: '申请成功',
            content: '申请成功，请耐心等待，我们会尽联系你',
            success: function (res) {
              if (res.cancel) {
                //点击取消,默认隐藏弹框
              } else {
                //点击确定
                wx.navigateBack({
                  delta: 1
                })
              }
            }
          })
        }else{
         wx.showModal({
           title: '错误提示',
           content: res.msg,
         })
        }
      });
    }
    else{
      wx.navigateTo({
        url: '../../login/login',
      })
    }
  },

  begin(e){
    if (this.data.checke==1){
    this.setData({
      checke : 0
    })
    }else{
      this.setData({
        checke: 1
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({

      phone: wx.getStorageSync("phone")
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})